package Part4_缓冲流;

import org.junit.Test;
import java.io.*;

/**
 * ClassName: BufferedTest
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.04.22 17:02
 */
public class BufferedTest {

    /**
     * 使用BufferedInputStream和BufferedOutputStream复制图片
     */
    @Test
    public void test1(){
        //1、创建File类对象
        File file1 = new File("640.jpg");
        File file2 = new File("copy_1.jpg");

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //2、创建输入、输出流
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            //false-->等同于不写，true-->>追加模式
            //fos = new FileOutputStream(file2, false);
            //fos = new FileOutputStream(file2, true);

            //3、创建缓冲流，包裹输入输出流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            //4、复制操作
            byte[] buffer = new byte[1024];//1KB
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //先关闭外层的传冲流
            //外层流关闭会自动关闭内层流
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 使用BufferedReader打印指定文本
     * 方式1：read(char[] cBuffer)
     */
    @Test
    public void test2(){
        File file1 = new File("hello.txt");

        BufferedReader br = null;

        try {
            //匿名FileReader对象
            br = new BufferedReader(new FileReader(file1));

            //读取过程
            char[] cBuffer = new char[100];
            int len;//记录每次读入到cBuffer中的字符个数
            while ((len=br.read(cBuffer))!=-1){
                //方法1:
                /*for(int i=0; i<len; i++){
                    System.out.print(cBuffer[i]);
                }*/
                //方法2:
                System.out.println(new String(cBuffer, 0, len));//char[]-->String
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 使用BufferedReader打印指定文本
     * 方式2：readLine()
     */
    @Test
    public void test3(){
        File file1 = new File("hello.txt");

        BufferedReader br = null;

        try {
            //匿名FileReader对象
            br = new BufferedReader(new FileReader(file1));

            //读取过程
            String date;
            while ((date = br.readLine())!=null){
                //br.readLine()方法不返回换行符
                //读到末尾时返回null
                System.out.println(date);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 使用BufferedReader和BufferedWriter复制文本
     */

    @Test
    public void test4(){
        //1、创建File类对象
        File file1 = new File("hello.txt");
        File file2 = new File("copy_2.txt");

        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            //2、创建输入、输出流
            FileReader fis = new FileReader(file1);
            FileWriter fos = new FileWriter(file2);

            //3、创建缓冲流，包裹输入输出流
            br = new BufferedReader(fis);
            bw = new BufferedWriter(fos);

            //4、复制操作
            String date;
            while ((date = br.readLine()) != null) {
                //由于每次复制只复制一行，且不带有换行符
                //需在循环中append('\n')或者newLine()
                bw.write(date);
                //bw.append('\n');
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
